﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class JLTSAS : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        JLTSASData m_Data, m_DataX;
        public DataSet setUI=new DataSet();
        public DataSet dsParamaters;
        public DataRow dwABPOS;
        public INTEXDBC INTEXDBC;
        private DataRow dwUI;
        DataTable tableUI;
        private string fromdndate = "", enddndate = "",fdndate="",edndate="";
        private string fromdate = "", enddate = "",fdate="",edate="";     
        #endregion
        public JLTSAS()
        {
            InitializeComponent();
            this.Name = "取样汇总记录";
           
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new JLTSASData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("ARTICLE", typeof(string));
            tableUI.Columns.Add("STAFF", typeof(string));
            tableUI.Columns.Add("FROMDATE", typeof(string));
            tableUI.Columns.Add("ENDDATE", typeof(string));
        }

        private void InitDataBinding()
        {
             
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUI = tableUI.NewRow();
                InitDataBinding();
                txtARTICLE.Focus();
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return;

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataSet setResult = new DataSet();
                    setUI.Clear();
                    setUI.AcceptChanges();
                    string STAFF = txtSTAFF.Text.Trim();
                    string ARTICLE = txtARTICLE.Text.Trim();
                    if (fdate == "")
                    {
                        fdate = "20000000";
                    }
                    if (edate == "")
                    {
                        edate = "99999999";
                    }
                    if (STAFF == "")
                    {
                        STAFF = "AAA";
                    } 
                    dwUI["ARTICLE"] = ARTICLE;
                    dwUI["STAFF"] = STAFF;
                    dwUI["FROMDATE"] = fdate;
                    dwUI["ENDDATE"] = edate;
                    setResult = m_Data.BuildJLTSASData(dwUI);
                    setUI.Merge(setResult);
                    MasterDg.DataContext = setUI.Tables["SAS"];
                }
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
                
        private void btnEXCEL_Click(object sender, RoutedEventArgs e)
        {
            m_DataX = new JLTSASData(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            JLTSASReport m_rpt = new JLTSASReport(dicParamater);
            m_rpt.dsSource = setUI;
            m_rpt.PrintType = "OpenExcel";
            m_rpt.STAFF = txtSTAFF.Text.Trim();
            m_rpt.fdate = fromdate;
            m_rpt.edate = enddate;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("OpenExcel");
        }

                
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            btnQuery_Click(null,null);

        }
        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                enddate = "";
                enddate = m_Data.GetDate(sender);
                edate = enddate;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromdate = "";
                fromdate = m_Data.GetDate(sender);
                fdate = fromdate;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }


    }
}
